package com.cardone.common.io;

import com.cardone.context.CodeException;
import com.cardone.context.action.Run2Action;
import com.google.common.collect.Maps;
import lombok.Setter;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * Created by Administrator on 2014/12/3.
 */
public abstract class AbstractFsManager implements FsManager {
    @Setter
    protected Map<String, Run2Action<HttpServletResponse, String>> writeToMap;

    public AbstractFsManager() {
        writeToMap = Maps.newHashMap();

        writeToMap.put("xls,xlsx,doc,docx,ppt,pptx", (response, filename) -> {
            response.setContentType("application/x-msdownload");

            try {
                response.setHeader("content-disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            } catch (final UnsupportedEncodingException e) {
                throw new CodeException(e);
            }
        });
    }
}
